package com.Listeners;

import com.SelfHome.Main;
import com.SelfHome.Variable;
import com.Util.Home;
import com.Util.HomeAPI;
import com.Util.MySQL;
import com.Util.Util;
import java.io.File;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;









public class WorldBlockPlaceListener
  implements Listener
{
  public int getChunkAmount(String nbt, Chunk chunk) {
    int NowAmount = 0; int b; int i; BlockState[] arrayOfBlockState;
    for (i = (arrayOfBlockState = chunk.getTileEntities()).length, b = 0; b < i; ) { BlockState state = arrayOfBlockState[b];
      if (Util.getNBTString(state).toUpperCase().contains(nbt.toUpperCase()))
        NowAmount++; 
      b++; }
    
    return NowAmount;
  }

  
  @EventHandler(priority = EventPriority.HIGHEST)
  public void BlockCanBuildEvent2(BlockPlaceEvent event) {

    if (event.getPlayer().getName().toUpperCase().contains("AS-FAKEPLAYER") || 
      event.getPlayer().getName().toUpperCase().contains("[MINECRAFT]") || 
      event.getPlayer().getName().toUpperCase().contains("[MEKANISM]") || 
      event.getPlayer().getName().toUpperCase().contains("[IF]")) {
      return;
    }
    
    Player p = event.getPlayer();
    if (event.getBlock() == null) {
      return;
    }
    
    int level = 0;
    
    if (Variable.bungee) {
      if (MySQL.CheckIsAHome(p.getWorld().getName().replace(Variable.world_prefix, ""))) {
        level = Integer.valueOf(MySQL.getLevel(p.getWorld().getName().replace(Variable.world_prefix, ""))).intValue();
      } else {
        return;
      } 
    } else {
      File f2 = new File(Variable.Tempf, String.valueOf(p.getWorld().getName().replace(Variable.world_prefix, "")) + ".yml");
      if (!f2.exists()) {
        return;
      }
      
      YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(f2);
      level = yamlConfiguration.getInt("Level");
    } 
    int addradius = Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2;
    
    if (!Main.JavaPlugin.getConfig().getBoolean("CustomTileMax")) {
      return;
    }
    
    Block block = event.getBlock();
    String nbt = Util.getNBTString(block.getState());
    boolean check_contain = false;
    String contain_nbt = "";
    int MaxThisTile = 0;
    for (int d = 0; d < Main.JavaPlugin.getConfig().getStringList("TileList").size(); d++) {
      String[] temp = ((String)Main.JavaPlugin.getConfig().getStringList("TileList").get(d)).split("\\|");
      if (temp[0].equalsIgnoreCase("world"))
      {

        
        if (nbt.toUpperCase().contains(temp[1].toUpperCase())) {
          check_contain = true;
          contain_nbt = temp[1];
          MaxThisTile = Integer.valueOf(temp[2]).intValue();
          
          break;
        } 
      }
    } 
    if (!check_contain) {
      return;
    }


    
    int NowAmount = 0;
    
    Location topleft = p.getWorld().getSpawnLocation().clone();
    Location topright = p.getWorld().getSpawnLocation().clone();
    Location bottomleft = p.getWorld().getSpawnLocation().clone();
    Location bottomright = p.getWorld().getSpawnLocation().clone();
    
    topleft.setX(p.getLocation().getWorld().getSpawnLocation().getX() + ((level + 1) * addradius));
    topleft.setZ(p.getLocation().getWorld().getSpawnLocation().getZ() + ((level + 1) * addradius));
    
    topright.setX(p.getLocation().getWorld().getSpawnLocation().getX() + ((level + 1) * addradius));
    topright.setZ(p.getLocation().getWorld().getSpawnLocation().getZ() - ((level + 1) * addradius));
    
    bottomleft.setX(p.getLocation().getWorld().getSpawnLocation().getX() - ((level + 1) * addradius));
    bottomleft.setZ(p.getLocation().getWorld().getSpawnLocation().getZ() + ((level + 1) * addradius));
    
    bottomright.setX(p.getLocation().getWorld().getSpawnLocation().getX() - ((level + 1) * addradius));
    bottomright.setZ(p.getLocation().getWorld().getSpawnLocation().getZ() - ((level + 1) * addradius));
    
    for (Chunk temp : Util.getchunkmap(topleft, topright, bottomleft, bottomright)) {
      NowAmount += getChunkAmount(contain_nbt, temp);
    }

    
    NowAmount--;

    
    boolean extra_perm = false;
    int extra_amount = MaxThisTile;
    for (int i = 100; i > 0; i--) {
      if (p.hasPermission("SelfHome.WorldPlace." + contain_nbt + "." + i)) {
        extra_perm = true;
        if (extra_amount < i) {
          extra_amount = i;
          
          break;
        } 
      } 
    }
    
    
    Home home = HomeAPI.getHome(event.getBlock().getWorld().getName().replace(Variable.world_prefix, ""));
    


    
    
    
    for(String str:home.getLimitBlock()) {
    	String[] args = str.split("\\|");
    	
    	
    	
    	int start_amount = 0;
    	for(int e=0;e<Main.JavaPlugin.getConfig().getStringList("TileList").size();e++) {
    		String[] temp = Main.JavaPlugin.getConfig().getStringList("TileList").get(e).split("\\|");
    		if(temp[0].equalsIgnoreCase("world") && nbt.toUpperCase().contains(temp[1].toUpperCase())) {
    			start_amount = Integer.valueOf(temp[2]);
    			break;
    		}
    	}
    	
    	
    	if(args[0].equalsIgnoreCase("world") && nbt.toUpperCase().contains(args[1].toUpperCase())) {
    		int amount = Integer.valueOf(args[2]) + start_amount;
    		if(extra_amount < amount) {
    			extra_perm = true;
    			extra_amount = amount;
    		}
    	}
    }
    
    
    
    
    for(String se:Main.JavaPlugin.getConfig().getStringList("AnotherChunkLimit")) {
    	String[] args = se.split("\\|");
        if (nbt.toUpperCase().contains(args[0].toUpperCase()) && home.getLevel() >= Integer.valueOf(args[1]) && Integer.valueOf(args[2]) >= extra_amount) {
        	extra_perm=true;
        	extra_amount = Integer.valueOf(args[2]);
    	}
    }
    
    if (extra_perm) {
      MaxThisTile = extra_amount;
    }


    
    if (Main.JavaPlugin.getConfig().getBoolean("EnableClearExtraBlocks") && 
      NowAmount + 1 > MaxThisTile) {
      event.setCancelled(true);
      int wait_to_delete = NowAmount + 1 - MaxThisTile - 1;
      for (Chunk temp : Util.getchunkmap(topleft, topright, bottomleft, bottomright)) {
        int b; int j; BlockState[] arrayOfBlockState; for (j = (arrayOfBlockState = temp.getTileEntities()).length, b = 0; b < j; ) { BlockState state = arrayOfBlockState[b];
          if (Util.getNBTString(state).toUpperCase().contains(contain_nbt.toUpperCase())) {
            if (wait_to_delete != 0) {
              state.getBlock().getLocation().getWorld().getBlockAt(state.getBlock().getLocation()).setType(Material.AIR);
              p.sendMessage(Variable.Lang_YML.getString("ClearExtraBlocks"));
              wait_to_delete--;
            } else {
              break;
            } 
          }

          
          b++; }
      
      } 
    } 

    
    if (NowAmount + 1 <= MaxThisTile) {
      String temp = Variable.Lang_YML.getString("PlaceWorldMaxTile");
      if (temp.contains("<Now>")) {
        temp = temp.replace("<Now>", String.valueOf(NowAmount + 1));
      }
      if (temp.contains("<Max>")) {
        temp = temp.replace("<Max>", String.valueOf(MaxThisTile));
      }
      if (temp.contains("<NBT>")) {
        temp = temp.replace("<NBT>", String.valueOf(contain_nbt));
      }
      p.sendMessage(temp);
    } else {
      String temp = Variable.Lang_YML.getString("PlaceReachWorldMaxTile");
      if (temp.contains("<Now>")) {
        temp = temp.replace("<Now>", String.valueOf(NowAmount));
      }
      if (temp.contains("<Max>")) {
        temp = temp.replace("<Max>", String.valueOf(MaxThisTile));
      }
      if (temp.contains("<NBT>")) {
        temp = temp.replace("<NBT>", String.valueOf(contain_nbt));
      }
      
      event.setCancelled(true);
      p.sendMessage(temp);
    }
  }
}


